//
//  ForgetCodeViewController.m
//  PatientApp
//
//  Created by chirui on 2017/5/17.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "ForgetCodeViewController.h"

@interface ForgetCodeViewController ()
@property (weak, nonatomic) IBOutlet UIView *phoneView;
@property (weak, nonatomic) IBOutlet UIView *codeView;
@property (weak, nonatomic) IBOutlet UIView *passwordView;
@property (weak, nonatomic) IBOutlet UIView *surePasswordView;
@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UITextField *codeText;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UITextField *surePasswordText;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@end

@implementation ForgetCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
//    self.navigationItem.titleView = [TopLabel withLabelWithLabelText:@"忘记密码"];
    
    
    self.sureBtn.layer.cornerRadius = 25.f;
    self.sureBtn.layer.masksToBounds = YES;
    
//    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_return" target:self action:@selector(back)];
    
}
- (IBAction)backClick:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

//-(void)back{
//    
////    [self.navigationController popViewControllerAnimated:YES];
//    
//    [self dismissViewControllerAnimated:YES completion:^{
//       
//    }];
//}

-(void)layerCornerRadiusView:(UIView *)view {
    
    view.layer.cornerRadius = 22.5f;
    view.layer.masksToBounds = YES;
    view.layer.borderWidth = 0.5;
    view.layer.borderColor = [UIColor grayColor].CGColor;
}

- (IBAction)codeBtnClick:(UIButton *)sender {
    
    if ([SHJudgeTools valiMobile:self.phoneText.text] != nil) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.yOffset = 100.f;
        hud.labelText = [SHJudgeTools valiMobile:self.phoneText.text];
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:0.5];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [hud removeFromSuperview];
        });
        return;
    }
    
    [self sendMSNetWorkingFor];
    __block int timeout = 60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=1){
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.codeBtn setTitle:@"获取" forState:UIControlStateNormal];
                self.codeBtn.userInteractionEnabled = YES;
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSString  * time = [NSString stringWithFormat:@"%d(秒)",timeout];
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [self.codeBtn setTitle:time forState:UIControlStateNormal];
                [UIView commitAnimations];
                self.codeBtn.userInteractionEnabled = NO;
               
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);

    
    
}

- (IBAction)sureBtnClick:(UIButton *)sender {
    
    if ([SHJudgeTools valiMobile:self.phoneText.text] != nil) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.yOffset = 100.f;
        hud.labelText = [SHJudgeTools valiMobile:self.phoneText.text];
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:0.5];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [hud removeFromSuperview];
        });
        return;
    }else if (self.phoneText.text.length!=11) {
        
        [SHJudgeTools MBProgressHUDPromptWith:@"手机号码不正确" On:self.view];
        return;
        
    }else if(_codeText.text.length<1){
        
        [SHJudgeTools MBProgressHUDPromptWith:@"验证码不能为空" On:self.view];
        return;
        
    }else if(_passwordText.text.length<1){
        [SHJudgeTools MBProgressHUDPromptWith:@"密码不能为空" On:self.view];
        return;
    }else if(_passwordText.text.length>8){
        [SHJudgeTools MBProgressHUDPromptWith:@"密码不能大于8位" On:self.view];
        return;
    }else if (![_passwordText.text isEqualToString:_surePasswordText.text]){
        
        [SHJudgeTools MBProgressHUDPromptWith:@"两次密码不一致" On:self.view];
        return;
        
    }else {
        
        [self findPasswordNetWorking];
    }

}


//找回密码
-(void)findPasswordNetWorking{
    
     NSString * md5ver = [SHJudgeTools md532BitLower:[NSString stringWithFormat:@"%@api.shangpibao.com",_phoneText.text]];
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",address_url,find_password_api];
    NSDictionary * parameter = @{@"uphone":_phoneText.text,
                                 @"upasswords":[SHJudgeTools md532BitLower:_passwordText.text],
                                 @"smscode":[SHJudgeTools md532BitLower:_codeText.text],
                                 @"ver":md5ver
                                 };
    NSLog(@"param ==%@  parametersStr = %@",parameter,parametersStr);
    
    SHuiMBhubNoTimeStr(@"正在加载");
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:parameter  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"err"] intValue]==0) {
            SHuiMBhudTextTimeStr(@"设置成功");
            
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW , (1 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
                    
                    [self.navigationController popViewControllerAnimated:YES];
                });
            });

        } else {
            
            SHuiMBhudTextTimeStr(EorrMessageStr);
        }
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"请检查您的网络!");
    }];
}

// 验证码
-(void)sendMSNetWorkingFor{
    
     NSString * md5ver = [SHJudgeTools md532BitLower:[NSString stringWithFormat:@"%@api.shangpibao.com",_phoneText.text]];
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@?uphone=%@&ver=%@",address_url,find_passwdSMS_api,_phoneText.text,md5ver];
    
    SHuiMBhubNoTimeStr(@"正在发送");
    [PostNetWorking netWorkingStr:parametersStr  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"code"] intValue]==200) {
            SHuiMBhudTextTimeStr(@"发送成功");
            
        } else {
            
            SHuiMBhudTextTimeStr(EorrMessageStr);
        }
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"请检查您的网络!");
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
